package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.tasks.BootTask;
import com.njrsrc.android.tasks.LoginTask;
import com.njrsrc.android.tasks.ShowUserInfoTask;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 启动页面
 * 
 * @author Administrator
 * 
 */
public class BootActivity extends BaseActivity {
	private UserInfo userInfo;
	private ProgressDialog progressDialog;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Intent mainIntent = new Intent();
			mainIntent.setClass(BootActivity.this, MainActivity.class);
			switch (msg.what) {
			case MsgID.BOOT_COMPLETE:
				// 进行自动登录,发送登录请求
				String username = preferences.getString("user", "");
				String password = preferences.getString("password", "");
				if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
					// 之前登录过
					List<String> params = new ArrayList<String>();
					params.add(username);
					params.add(password);
					String url = Helpers.combinaStr(RequestUrlProvider.USER_LOGIN, params);
					userInfo = new UserInfo();
					Object[] args = new Object[3];
					args[0] = mHandler;
					args[1] = url;
					args[2] = userInfo;
					progressDialog = Helpers.showProgressDialog(BootActivity.this, R.string.logining);
					new LoginTask().execute(args);
				} else {
					startActivity(mainIntent);
					finish();
				}
				break;
			case MsgID.LOGIN_ERROR:
				// 登录失败
				Helpers.closeProgressDialog(progressDialog);
				Helpers.showToast(BootActivity.this, userInfo.getError_message(), Toast.LENGTH_SHORT);
				startActivity(mainIntent);
				finish();
				break;
			case MsgID.LOGIN_FAIL:
				// 登录失败
				Helpers.closeProgressDialog(progressDialog);
				Helpers.showToast(BootActivity.this, userInfo.getError_message(), Toast.LENGTH_SHORT);
				startActivity(mainIntent);
				finish();
				break;

			case MsgID.LOGIN_SUCCESS:
				// 登录成功
				Helpers.closeProgressDialog(progressDialog);
				Helpers.showToast(BootActivity.this, R.string.loginSuc, Toast.LENGTH_SHORT);
				AppApplication.mUserID = userInfo.getUser_id();
				AppApplication.mToken = userInfo.getToken();
				AppApplication.mResumeID = userInfo.getResume_id();
				AppApplication.isLogin = true;
				// 获取用户信息
				List<String> params = new ArrayList<String>();
				params.add(AppApplication.mToken);
				params.add(AppApplication.mUserID);
				String url = Helpers.combinaStr(RequestUrlProvider.USER_SHOW, params);
				Object[] args = new Object[3];
				args[0] = mHandler;
				args[1] = url;
				args[2] = userInfo;
				new ShowUserInfoTask().execute(args);
				break;

			case MsgID.SHOW_USER_INFO_FAIL:
				// 获取用户信息失败
				startActivity(mainIntent);
				finish();
				break;

			case MsgID.SHOW_USER_INFO_SUCCESS:
				// 获取用户信息成功
				AppApplication.mUserName = userInfo.getLogin_name();
				AppApplication.mHeadUrl = userInfo.getPic_url();
				AppApplication.hrLetterCount = userInfo.getHr_letter_count();
				startActivity(mainIntent);
				finish();
				break;
			default:
				break;
			}
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_boot);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Object[] objs = new Object[2];
		objs[0] = context;
		objs[1] = mHandler;
		new BootTask().execute(objs);
	}
}